from flask import Flask, request;
from flask_cors import CORS;
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
import requests as r;
from uuid import uuid4 as uuid

# gobal threadPool
pool = ThreadPoolExecutor()

# 拿到图片url
def get_url(url):
    html = r.get(url)
    soup  = BeautifulSoup(html.content, "lxml")
    print(soup.find(class_="main-content")["src"])
    return soup.find(class_="main-content")["src"]

def download_image(url):
    print(url)
    img = r.get(url).content
    # url.split('.')[-1]} 是为了得到正常后缀
    with open(f"./images/{uuid()}.{url.split('.')[-1]}", "wb") as f:
        f.write(img)
    

def craw(url):
    download_image(get_url(url))
    return "ok"

app = Flask(__name__)

# 跨域非必须
CORS(app, resources=r"/*")

@app.route("/",methods=['post'])
def index():
    results = pool.map(craw,request.json)
    cnt = 0
    for item in results:
        cnt += 1
        print("one is done")
    print(f"over {cnt}")
    
    return "ok"

if __name__ == "__main__":
    app.run(debug=True)
    